var req = null;

function init(){
	var rental = $("input.rental");
	var rentalLength = rental.length;
	
	for(var i=0; i<rentalLength; i++){
		rental[i].onclick = function(){
			var bookId = this.getAttribute("id");
			updateContent(contextPath + "/rentalYn.do?book_id=" + bookId, bookId);
		};
	}
}

function getXHR(){
	
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	}else {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				return null;
			}
		}
	}
}

function updateContent(url, id){
	
	req = getXHR();
	
	if(req != null){
		
		req.open("get", url);

		req.onreadystatechange = function(){
			
			if(req.readyState == 4){
				if(req.status == 200){
					var result = document.createTextNode(req.responseText);
					var rentalYn = $("div.rentalYn_" + id)[0];
					
					if(rentalYn.childNodes.length == 0){
						var temp = result.nodeValue;
						var resultArr = temp.split("$");
						
						for(var i=0; i<resultArr.length; i++){
							var p = document.createElement("p");
							var textNode = document.createTextNode(resultArr[i]);
							p.appendChild(textNode);
							rentalYn.appendChild(p);
						}
					}else{
						
					}
					
				}else{
					
				}
			}	
		};
		
		req.send('');
	}
}


$(window).load(function() {
	init();
});